import { Router } from 'express';
import { authenticate, authorize } from '../../middleware/auth';
import { PermissionController } from '../../admin/controllers/permissionController';
import { logOperation } from '../../middleware/operationLog';
import { ALL_PERMISSIONS } from '../../constants/permissions';
import { skipLog } from '../../middleware/operationLog';

const router = Router();
const permissionController = new PermissionController();
/**
 * @swagger
 * /api/permissions/stats:
 *   get:
 *     summary: 获取权限统计信息
 *     tags: [权限管理]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 */
router.get('/stats', authenticate, authorize([ALL_PERMISSIONS.PERMISSION_READ]), skipLog, permissionController.getPermissionStats);

/**
 * @swagger
 * /api/permissions/modules:
 *   get:
 *     summary: 获取权限模块列表
 *     tags: [权限管理]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 */
router.get('/modules', authenticate, authorize([ALL_PERMISSIONS.PERMISSION_READ]), skipLog, permissionController.getPermissionModules);

/**
 * @swagger
 * /api/permissions/tree:
 *   get:
 *     summary: 获取权限树形结构
 *     tags: [权限管理]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 */
router.get('/tree', authenticate, authorize([ALL_PERMISSIONS.PERMISSION_READ]), skipLog, permissionController.getPermissionTree);

/**
 * @swagger
 * /api/permissions/batch:
 *   post:
 *     summary: 批量创建权限
 *     tags: [权限管理]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - permissions
 *             properties:
 *               permissions:
 *                 type: array
 *                 items:
 *                   type: object
 *                   properties:
 *                     name:
 *                       type: string
 *                       description: 权限名称
 *                     code:
 *                       type: string
 *                       description: 权限代码
 *                     description:
 *                       type: string
 *                       description: 权限描述
 *                     module:
 *                       type: string
 *                       description: 权限模块
 *     responses:
 *       201:
 *         description: 批量创建成功
 */
router.post('/batch', 
  authenticate, 
  authorize([ALL_PERMISSIONS.PERMISSION_CREATE]),
  logOperation('权限批量创建', '权限管理'),
  permissionController.batchCreatePermissions
);

/**
 * @swagger
 * /api/permissions/{id}:
 *   get:
 *     summary: 根据ID获取权限
 *     tags: [权限管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 权限ID
 *     responses:
 *       200:
 *         description: 获取成功
 *   put:
 *     summary: 更新权限
 *     tags: [权限管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 权限ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *                 description: 权限名称
 *               code:
 *                 type: string
 *                 description: 权限代码
 *               description:
 *                 type: string
 *                 description: 权限描述
 *               module:
 *                 type: string
 *                 description: 权限模块
 *     responses:
 *       200:
 *         description: 更新成功
 *   delete:
 *     summary: 删除权限
 *     tags: [权限管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 权限ID
 *     responses:
 *       200:
 *         description: 删除成功
 */
router.get('/:id', authenticate, authorize([ALL_PERMISSIONS.PERMISSION_READ]), skipLog, permissionController.getPermissionById);
router.put('/:id', 
  authenticate, 
  authorize([ALL_PERMISSIONS.PERMISSION_UPDATE]),
  logOperation('权限更新', '权限管理'),
  permissionController.updatePermission
);
router.delete('/:id', 
  authenticate, 
  authorize([ALL_PERMISSIONS.PERMISSION_DELETE]),
  logOperation('权限删除', '权限管理'),
  permissionController.deletePermission
);


/**
 * @swagger
 * /api/permissions:
 *   get:
 *     summary: 获取权限列表
 *     tags: [权限管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 20
 *         description: 每页数量
 *       - in: query
 *         name: search
 *         schema:
 *           type: string
 *         description: 搜索关键词
 *       - in: query
 *         name: module
 *         schema:
 *           type: string
 *         description: 权限模块
 *   post:
 *     summary: 创建权限
 *     tags: [权限管理]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - code
 *             properties:
 *               name:
 *                 type: string
 *                 description: 权限名称
 *               code:
 *                 type: string
 *                 description: 权限代码
 *               description:
 *                 type: string
 *                 description: 权限描述
 *               module:
 *                 type: string
 *                 description: 权限模块
 *     responses:
 *       201:
 *         description: 创建成功
 */
router.get('/', authenticate, authorize([ALL_PERMISSIONS.PERMISSION_READ]), skipLog, permissionController.getPermissions);
router.post('/', 
  authenticate, 
  authorize([ALL_PERMISSIONS.PERMISSION_CREATE]),
  logOperation('权限创建', '权限管理'),
  permissionController.createPermission
);

export default router; 